<template>
  <div style="width: 100%">
    <a-select
      :disabled="disabled"
      show-search
      :value="value"
      :placeholder="placeholder"
      style="min-width: 150px;max-width:30vw;"
      :default-active-first-option="false"
      :show-arrow="false"
      :filter-option="false"
      :not-found-content="null"
      allowClear
      @search="handleSearch"
      @change="handleChange"
      @focus="handleSearch"
    >
      <a-select-option v-for="d in data" :key="d.name">
        {{ d.name }}
      </a-select-option>
    </a-select>
  </div>
</template>

<script>
import {
  getSchoolDownList,
  getSchoolCollegesDownList,
  getSchoolSpecialtyDownList,
  getTianyanchaSearch,
  getTianyanchaDetail
} from '@/api/inputSearch'
import { InterfaceTypeEnum } from '@/enums/isInternalInterface'
import { isObject } from 'lodash-es'
export default {
  props: ['value', 'placeholder', 'isInternalInterface', 'formGroupId', 'id', 'disabled'],
  computed: {
    api() {
      switch (this.isInternalInterface) {
        case InterfaceTypeEnum.TianYanCha:
          return getTianyanchaSearch
        case InterfaceTypeEnum.School:
          return getSchoolDownList
        case InterfaceTypeEnum.Department:
          return getSchoolCollegesDownList
        case InterfaceTypeEnum.Major:
          return getSchoolSpecialtyDownList
      }
    }
  },
  data() {
    return {
      data: []
    }
  },
  methods: {
    isObject,
    async handleSearch(keyword) {
      const res = await this.api(keyword || this.value)
      if (res.code) return
      this.data = res.data.records
    },
    async handleChange(value) {
      let newVal = value
      if (this.isInternalInterface === InterfaceTypeEnum.TianYanCha&&newVal) {
        const item = this.data.find(item => item.name === value)
        if (!item) return
        const res = await getTianyanchaDetail(item)
        if (res.code) return
        newVal = res.data
      }
      this.$emit('change', {
        value: isObject(newVal) ? newVal.company_name : newVal,
        tianYanChaInfo: newVal,
        formGroupId: this.formGroupId,
        id: this.id,
        isInternalInterface: this.isInternalInterface
      })
    }
  }
}
</script>
